package com.binvshe.binvshe.organization;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.binvshe.binvshe.R;
import com.binvshe.binvshe.activity.BannerEntity;
import com.binvshe.binvshe.activity.LocalImageHolderView;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.organization.entity.OpusListEntity;
import com.binvshe.binvshe.organization.entity.OrgTypeEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.frameworkbase.utils.DimensionUtil;

import java.util.List;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/8/25
 * 首页社团页面
 */
public class OrganizationFragment extends SwipeListFragment {

    private ConvenientBanner convenientBanner;
    private RecyclerView recButtons;

    TypeAdapter mTypeAdapter;
    private int mPage = 0;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        View headerView = inflater.inflate(R.layout.item_org_header, container, false);
        this.recButtons = (RecyclerView) headerView.findViewById(R.id.recButtons);
        this.convenientBanner = (ConvenientBanner) headerView.findViewById(R.id.convenientBanner);
        getBannerData("club");
        hideToolbar();
//        showToolbar();
        setTitle("社团");
        mTypeAdapter = new TypeAdapter(getContext());
        recButtons.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        recButtons.setAdapter(mTypeAdapter);
        getOrgType();
        getAdapter().setHeaderView(headerView);
        setPaddingTop(getActivity(), getRootView());
        return rootView;
    }

    public static void setPaddingTop(Context context, View rootView) {
        int statusBarHeight = DimensionUtil.dip2px(context, 25);
        rootView.setPadding(0, statusBarHeight, 0, 0);
    }

    @Override
    public void onResume() {
        super.onResume();
        convenientBanner.startTurning(5000);
        onStartLoading();
    }

    @Override
    public void onPause() {
        super.onPause();
        convenientBanner.stopTurning();
    }

    public OrganizationFragment() {
        setRetainInstance(true);  //转屏不需要重新创建
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new OpusAdapter(getActivity());
    }

    @Override
    public void onStartLoading() {
        getRecommendList();
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        mPage = 0;
        getData().clear();
        onStartLoading();
    }
    ListDataRequestListener<OpusListEntity> mRequestListener = new ListDataRequestListener(this, OpusListEntity.class, true);

    /**
     * 获取首页推荐列表
     */
    private void getRecommendList() {
        if (mPage == 0 || mRequestListener.getResponseDataParser().hasMore()) {
            mPage++;
            IHttpRequest request = new MyHttpRequest(getContext());
            UrlParams params = new UrlParams();
            params.put("page", mPage + "");
            request.get(UrlCenter.GET_CLUB_RECOMMEND, params, mRequestListener);
        }
    }

    //设置banner数据
    private void setBannerData(List<BannerEntity> bannerData) {
        convenientBanner.setPages(
                new CBViewHolderCreator<LocalImageHolderView>() {
                    @Override
                    public LocalImageHolderView createHolder() {
                        return new LocalImageHolderView();
                    }
                }, bannerData).setPageIndicator(new int[]{R.mipmap.icon_nav_n, R.mipmap.icon_nav_p})
                //设置指示器的方向
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
    }



    /**
     * 获取banner数据
     */
    private void getBannerData(String type) {
        MyHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.BANNER_TYPE, type);
        request.get(url, null, new DataRequestListener<List<BannerEntity>>(BannerEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(List<BannerEntity> bannerEntity) {
                super.onSuccess(bannerEntity);
                setBannerData(bannerEntity);

            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }



    /**
     * 获取可选的类型
     */
    private void getOrgType() {
        IHttpRequest request = new MyHttpRequest(getContext());
        request.get(UrlCenter.GET_ORG_TYPE, null, new DataRequestListener<List<OrgTypeEntity>>(OrgTypeEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(List<OrgTypeEntity> o) {
                super.onSuccess(o);
                mTypeAdapter.getData().clear();
                mTypeAdapter.getData().addAll(o);
                mTypeAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }





}
